/**
 * Main.java created by zhangzhidong 
 * at 下午5:37:36 2015年8月21日
 */
package queue;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;

/**
 * @author Alex
 *
 */
class Apple{
	int index;
	Apple(int idx){
		this.index=idx;
		System.out.print( "Produce An apple :"+ index+" " );
		System.out.println();
	}
	public String toString(){
		return " Apple: " + index+" ";
	}
}
public class Main {
	private MyBlockingQueue myqueue = new MyBlockingQueue(10);
	AtomicInteger index = new AtomicInteger(0);
	class Producer implements Runnable{
		public void run() {
			// TODO Auto-generated method stub
			while(true){
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					System.out.print(Thread.currentThread().getName()+" ");
					myqueue.enQueue(new Apple(index.getAndIncrement()));
					System.out.println();
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	class Consumer implements Runnable{
		
			public void run() {
				
				while(true){

					try {
						Thread.currentThread().sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
							System.out.println(Thread.currentThread().getName()+" deQueue: "+(Apple)myqueue.deQueue());
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
				}		
			}
		
	}
	
	
	public Main()	{
		for(int i=0;i<1;i++){
			new Thread(new Consumer(),"Consumer-Thread-"+i).start();
		}
		for(int i=0;i<10;i++){
			new Thread(new Producer(),"Producer-Thread-"+i).start();
		}
		
	}
	
	/**
	 * 下午5:37:36 2015年8月21日
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main();
	}

}
